package top.lyjwn.ajui.listener;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import top.lyjwn.ajui.util.LogUtil;

public class IWebSocketListener extends WebSocketListener {
    protected Context context;

    public IWebSocketListener(Context context) {
        this.context = context;
    }

    @Override
    public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
        super.onClosed(webSocket, code, reason);
        LogUtil.d("webSocket closed", "已关闭");
    }

    @Override
    public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
        super.onClosing(webSocket, code, reason);
        LogUtil.d("webSocket closing", "正在关闭");
    }

    @Override
    public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
        super.onFailure(webSocket, t, response);
        LogUtil.e("webSocket failure", "故障",t);
    }

    @Override
    public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
        super.onMessage(webSocket, text);
        LogUtil.d("webSocket onMessage", text);
        Intent intent_connect_status = new Intent("top.lyjwn.ajui.receiver.ChatDialogReceiver");
        intent_connect_status.putExtra("data", text);
        context.sendOrderedBroadcast(intent_connect_status, null);
    }


    @Override
    public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
        super.onOpen(webSocket, response);
        LogUtil.d("webSocket open", "打开");
    }

}
